﻿namespace WMS6.Web.Outbound
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.OutboundService2Ref;

    public class WaveFlow : BasePage
    {
        protected Button btnAddDetail;
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnDeleteDetail;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected CheckBox chkSystemCreated;
        protected HtmlForm F7161;
        protected GridView gvWaveFlow;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnCopy;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnLineID;
        protected HtmlInputHidden hdnWaveFlowID;
        protected HtmlHead Head1;
        protected Label lblDescr;
        protected Label lblMsg;
        protected Label lblWaveFlow;
        protected Label lblWaveFlowID;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected TextBox txtDescr;
        protected TextBox txtWaveFlowID;

        protected void btnAddDetail_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.hdnFlag.Value != "1")
                {
                    this.lblMsg.Text = "没有保存,不能新增明细!";
                }
                else
                {
                    base.Response.Redirect(string.Format("WaveFlowDetail.aspx?from={0}&ID={1}&SeqNO={2}&type={3}", new object[] { base.UrlEncode(this.hdnFrom.Value), base.UrlEncode(this.txtWaveFlowID.Text.Trim()), base.UrlEncode(this.hdnLineID.Value), base.UrlEncode("New"), false }));
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            if (this.hdnCopy.Value != "0")
            {
                this.lblMsg.Text = "已经是复制状态,无法再复制!";
            }
            else
            {
                this.hdnFlag.Value = "0";
                this.hdnCopy.Value = "1";
                this.txtWaveFlowID.Enabled = true;
                this.txtWaveFlowID.Text = "";
                this.chkSystemCreated.Checked = false;
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.hdnFlag.Value != "1")
            {
                this.lblMsg.Text = "没有保存不能删除！";
            }
            else
            {
                try
                {
                    List<string> WaveFlowID = new List<string> {
                        this.txtWaveFlowID.Text.Trim()
                    };
                    this.lblMsg.Text = "删除波次流程 " + OutboundService2ClientProxy.GetInstance().DeleteWaveFlowInfo(WaveFlowID) + "成功!";
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = ex.Message;
                }
            }
        }

        protected void btnDeleteDetail_Click(object sender, EventArgs e)
        {
            if (this.hdnFlag.Value != "1")
            {
                this.lblMsg.Text = "没有保存,不能删除明细!";
            }
            else
            {
                try
                {
                    List<string> lineIDs = base.GetSelectedIDsInGridView(this.gvWaveFlow, "LineID");
                    List<string> waveFlowIDs = new List<string>();
                    if ((lineIDs == null) || (lineIDs.Count == 0))
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F7100_019");
                    }
                    else
                    {
                        for (int i = 0; i < lineIDs.Count; i++)
                        {
                            waveFlowIDs.Add(this.txtWaveFlowID.Text.Trim());
                        }
                        this.lblMsg.Text = OutboundService2ClientProxy.GetInstance().DeleteWaveFlowDetailInfo(waveFlowIDs, lineIDs) + base.GetGlobalMsgResource("F7100_014");
                        this.SetWaveFlowInfo2Form();
                    }
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7100_015") + ex.Message;
                }
                finally
                {
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
                }
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            base.Response.Redirect("WaveFlowList.aspx?from=edit", false);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Exception ex;
            try
            {
                WaveFlowInfo waveFlowInfo = null;
                if (base.DataEntityViewState != null)
                {
                    waveFlowInfo = base.DataEntityViewState as WaveFlowInfo;
                }
                else
                {
                    waveFlowInfo = new WaveFlowInfo();
                }
                waveFlowInfo.Descr = this.txtDescr.Text.Trim();
                waveFlowInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                if (this.hdnFlag.Value == "1")
                {
                    waveFlowInfo.WaveFlowID = this.txtWaveFlowID.Text.Trim();
                    waveFlowInfo.UpdatedBy = base.UserInfo.UserID;
                    this.lblMsg.Text = "更新" + OutboundService2ClientProxy.GetInstance().UpdateWaveFlowInfo(waveFlowInfo) + "成功";
                }
                else
                {
                    try
                    {
                        waveFlowInfo.WaveFlowID = this.txtWaveFlowID.Text.Trim();
                        waveFlowInfo.CreatedBy = base.UserInfo.UserID;
                        this.txtWaveFlowID.Text = OutboundService2ClientProxy.GetInstance().InsertWaveFlowInfo(waveFlowInfo);
                        this.lblMsg.Text = "新增波次流程" + this.txtWaveFlowID.Text + "成功";
                    }
                    catch (Exception exception1)
                    {
                        ex = exception1;
                        this.lblMsg.Text = ex.Message;
                        return;
                    }
                    this.hdnFlag.Value = "1";
                    this.hdnCopy.Value = "0";
                }
                this.hdnWaveFlowID.Value = this.txtWaveFlowID.Text;
                this.SetWaveFlowInfo2Form();
                base.DataEntityViewState = waveFlowInfo;
                this.txtWaveFlowID.Enabled = false;
            }
            catch (Exception exception2)
            {
                ex = exception2;
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void gvWaveFlow_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
            }
        }

        protected void gvWaveFlow_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                WaveFlowInfo waveFlowInfo = base.DataEntityViewState as WaveFlowInfo;
                DataView dv = CADataConverter.ToDataTable(waveFlowInfo.WaveFlowDetailInfos).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvWaveFlow, dv);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnWaveFlowID.Value = base.Request.QueryString["ID"];
                this.hdnFlag.Value = "1";
                this.SetWaveFlowInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["detail"]))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetWaveFlowInfo2Form()
        {
            string WaveFlowID = this.hdnWaveFlowID.Value;
            WaveFlowInfo waveFlowInfo = OutboundService2ClientProxy.GetInstance().GetWaveFlowInfoByID(WaveFlowID);
            base.DataEntityViewState = waveFlowInfo;
            if (waveFlowInfo != null)
            {
                this.txtWaveFlowID.Text = waveFlowInfo.WaveFlowID;
                this.txtDescr.Text = waveFlowInfo.Descr;
                this.chkSystemCreated.Checked = waveFlowInfo.SystemCreated == "Y";
                this.chkIsActive.Checked = waveFlowInfo.IsActive == "Y";
                if ((waveFlowInfo.WaveFlowDetailInfos != null) && (waveFlowInfo.WaveFlowDetailInfos.Count > 0))
                {
                    this.hdnLineID.Value = waveFlowInfo.WaveFlowDetailInfos[waveFlowInfo.WaveFlowDetailInfos.Count - 1].LineID.ToString();
                }
                else
                {
                    this.hdnLineID.Value = "0";
                }
                base.BindData2GridView(this.gvWaveFlow, waveFlowInfo.WaveFlowDetailInfos);
            }
            this.txtWaveFlowID.Enabled = false;
        }
    }
}

